package com.gitee.base.security;

import com.gitee.base.modules.BaseController;
import com.gitee.base.modules.core.model.SysPermExt;
import com.gitee.base.modules.core.model.SysUser;
import com.gitee.base.modules.core.service.ISysPermExtService;
import com.gitee.base.security.permission.Permission;
import com.gitee.base.security.permission.PermissionResolver;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class GlobalInterceptor extends BaseController implements HandlerInterceptor {

    @Autowired
    private PermissionResolver permissionResolver;
    @Autowired
    private ISysPermExtService sysPermExtService;

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if(modelAndView==null) return;
        SysUser loginSysUser = getLoginUser();
        if(loginSysUser !=null){
            modelAndView.addObject("loginUser", loginSysUser);

            if(loginSysUser.getRoleIds()!=null && StringUtils.isNotBlank(loginSysUser.getRoleIds())){
                List<Permission> permissionList = this.permissionResolver.permissionsByRoleIds(Arrays.stream(loginSysUser.getRoleIds().split(",")).mapToInt(Integer::valueOf).toArray());
                if(permissionList!=null && !permissionList.isEmpty()){
                    List<SysPermExt> sysPermExtList = this.sysPermExtService.list();
                    for(Permission permission:permissionList){
                        permission.setChildren(sortBySpecifiedSeq(permission.getChildren(),sysPermExtList));
                    }
                    sortBySpecifiedSeq(permissionList,sysPermExtList);
                    modelAndView.addObject("permissionList",permissionList);
                }
            }
        }

    }

    public static List<Permission> sortBySpecifiedSeq(List<Permission> permissions,List<SysPermExt> sysPermExts){
        if(permissions==null||permissions.isEmpty()||sysPermExts==null||sysPermExts.isEmpty()) return permissions;
        List<String> permIds = permissions.stream().map(Permission::getId).collect(Collectors.toList());
        List<String> sortIds = sysPermExts.stream().filter(s->permIds.contains(s.getId())).sorted(Comparator.comparingInt(SysPermExt::getSort)).map(SysPermExt::getId).collect(Collectors.toList());
        for(int sIdx=0;sIdx<sortIds.size();sIdx++){
            String id = sortIds.get(sIdx);
            for(int pIdx=0;pIdx<permissions.size();pIdx++){
                if(permissions.get(pIdx).getId().equals(id)){
                    Permission s = permissions.get(sIdx);
                    Permission p = permissions.get(pIdx);
                    permissions.set(sIdx,p);
                    permissions.set(pIdx,s);
                }
            }
        }
        return permissions;
    }
}

